<?php defined('BASE_PATH') or die('No direct script access.');
/* ------------------------------------------------------
 * Author : Gialac
 * File : db.class.php
 * Date : 27/11
 * Mysqli DB Class
  ------------------------------------------------------ */
class db {
    var $username;
    var $password;
    var $server;
    var $databasename;
    var $prefix;
    var $errormsg;
    var $connection;  // connection container
    var $query_result; // query result container
    var $row = array(); // row array result container for 1 row result query
    var $rowset = array(); // row array result container for multi row result query
//    private $registry;

    function __construct() {
        // set configuration from config file variables
//        $this->registry = $registry;
        $this->server = $GLOBALS['dbserver'];
        $this->username = $GLOBALS['dbuname'];
        $this->password = $GLOBALS['dbpasswd'];
        $this->databasename = $GLOBALS['dbname'];
        $this->prefix = $GLOBALS['dbprefix'];
        $this->errormsg = $GLOBALS['dberrormsg'];
        $this->connect();
        // make connection on Class start.
    }

    function __destruct() {
        // close connection
        if (isset($this->connection)) {
            $this->disconnect($this->connection);
        }
    }

    public function connect() {
        // connection establish
        $this->connection = mysqli_connect($this->server, $this->username, $this->password);
        // check connection
        if (!$this->connection) {
            $this->throwExceptionOnError($this->connection); // exception on error
        }
        else {
            // set charset for connection to utf-8
            mysqli_query($this->connection, "SET NAMES 'utf8'");
            mysqli_query($this->connection, "SET CHARACTER SET 'utf8'");
            mysqli_query($this->connection, "SET character_set_connection = 'utf8'");
            mysqli_set_charset($this->connection, "utf8");
            // end of charset correction
            // select database after charset correction
            mysqli_select_db($this->connection, $this->databasename);
            // connection establish
            return $this->connection;
        }
    }

    public function disconnect($linkIdentifier) {
        // close connection. also you can use in private
        if ($linkIdentifier == null) {
            $linkIdentifier = $this->connection;
        }
        $this->freeresult();
        return mysqli_close($linkIdentifier);
    }

    public function query($strQuery) {
        // unset older query result for make new
        unset($this->query_result);
        // check inputs
        if ($strQuery != "") {
            $this->query_result = mysqli_query($this->connection, $strQuery);
            if ($this->query_result) {
                // return result
                $this->query_result;
                return $this;
            }
            elseif (mysqli_error($this->connection)) {
                // error reporting for failed query and return false
                $this->throwExceptionOnError($this->connection);
                return false;
            }
        }
        else {
            return false;
        }
    }

    public function freeresult($query_id = 0) {
        // free point result for memory managment
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if (isset($query_id)) {
            if (isset($this->rowset))
                unset($this->rowset);
            if (isset($this->row))
                unset($this->row);
            @mysqli_free_result($query_id);
            return true;
        }
        else {
            return false;
        }
    }

    public function affected($query_id = 0) {
        // similar than mysqli_affected_rows but you dont need make new query for check
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        return ( $this->connection ) ? mysqli_affected_rows($this->connection) : false;
    }

    public function num_row($query_id = 0) {
        // process count of query result row without make new query
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        return ( $query_id ) ? mysqli_num_rows($query_id) : false;
    }

    public function num_fields() {
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        return ( $query_id ) ? mysqli_num_fields($query_id) : false;
    }

    public function list_fields() {
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        $field_names = array();
        while ($field = mysqli_fetch_field($query_id)) {
            $field_names[] = $field->name;
        }
        return $field_names;
    }

    public function fetch($query_id = 0) {
        // fetch and return first row value in array
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            $this->row = mysqli_fetch_assoc($query_id);
            return $this->row;
        }
        else {
            return false;
        }
    }

    public function fetchall($query_id = 0) {
        // fetch and return multiple roe record in array
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            while ($this->rowset = mysqli_fetch_assoc($query_id)) {
                $result[] = $this->rowset;
            }
            return $result;
        }
        else {
            return false;
        }
    }

    public function fetchrow($query_id = 0) {
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            $this->row = mysqli_fetch_row($query_id);
            return $this->row;
        }
        else {
            return false;
        }
    }

    public function fetchallrow($query_id = 0) {
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            while ($this->rowset = mysqli_fetch_row($query_id)) {
                $result[] = $this->rowset;
            }
            return $result;
        }
        else {
            return false;
        }
    }

    public function fetchobject($query_id = 0) {
        // fetch and return results as object for OOP usage
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            return mysqli_fetch_object($query_id);
        }
        else {
            return false;
        }
    }

    public function fetchallobject($query_id = 0) {
        if (!$query_id) {
            $query_id = $this->query_result;
        }
        if ($query_id) {
            while ($objset = mysqli_fetch_object($query_id)) {
                $result[] = $objset;
            }
            unset($objset);
            return $result;
        }
        else {
            return false;
        }
    }
    public function insert_id() {
        return @mysqli_insert_id($this->connection);
    }
    public function schema() {
        // return your database schema without style
        // this is great tools for you
        $schema = "";
        $query = "show tables";
        $result = $this->query($query);
        if ($result) {
            $schema .= "<ul><li>" . $this->databasename . ":<ul>";
            $table_list = $this->fetchallrow();
            foreach ($table_list as $table_name_list) {
                foreach ($table_name_list as $table_name) {
                    $schema .= "<li>" . $table_name . "<ul>";
                    $query = "describe " . $table_name;
                    $result = $this->query($query);
                    $field_list = $this->fetchallrow();
                    foreach ($field_list as $field_name_list) {
                        $schema .= "<li>" . $field_name_list[0] . " - " . $field_name_list[1] . "</li>";
                    }
                    $schema .= "</ul></li>";
                }
            }
            $schema .= "</ul></li></ul>";
        }
        return $schema;
    }
    public function escape($value) {
        return mysqli_real_escape_string($value, $this->connection);
    }
    public function throwExceptionOnError($link = null) {
        // connection error exception
        // error_reporting(E_ALL | E_STRICT);
        // ini_set('display_errors', 'on');
        if ($link == null) {
            $link = $this->connection;
        }

        if (mysqli_error($this->connection)) {
            $msg = mysqli_errno($this->connection) . ": " . mysqli_error($this->connection);
            if (isset($this->errormsg)) {
                throw  new Exception('Database Access Error: '. $this->errormsg);
            }
            else {
                throw new Exception('MySQL Error - ' . $msg);
            }
        }
    }

}
# End of file db.class.php